function [y,s,t] = myDijkstra(G,last)

% number of nodes
NodeNum = size(G,1);

% let unvisited nodes = -1
Visited = -1*ones(NodeNum,1);

% successor nodes (if disconnected, leave as 0)
s = zeros(NodeNum,1); %-1*ones(NodeNum,1);

% the target node in the graph 
target = last; %size(G,1);

% distances
y = Inf*ones(NodeNum,1);

% distance to target node is 0
y(target) = 0;

% begin with target node
CurrentNode = target;

% counter to keep track of number of visited nodes
nVisited = 0;

t = 0; % iter count

% apply process until all nodes have been visited
while nVisited < NodeNum
   
   % visited node: mark with 1
   Visited(CurrentNode) = 1;
   
   for i = 1:NodeNum
       
       % for edges concerning the current node
       if G(i,CurrentNode)>0 
           
           % add edge distance to temporary matrix temp
           
           temp = G(i,CurrentNode) + y(CurrentNode);
           
           % if the previous distance from node a to the target node
           % is GREATER than the the sum of (distance from a to b, distance
           % from b to target node), update a's distance.
           
           % if this distance is shorter than y(i), update y(i)

           if temp < y(i)
               
               % update
               y(i) = temp; 
               
               % record previous node with current working node
               s(i) = CurrentNode;
           
           end
           
       end
       
   end
   
   minvalue = 0;
   
   for i = 1:NodeNum       
       
       % if node i is unvisited (= -1) and its distance is positive
       if Visited(i)<0 && y(i)>0
           
           % if null minvalue, set equal to node i 
           if minvalue == 0
               
               minvalue = i;
           
           % if distance is less than the current 
           % shortest distance, update
           elseif y(i) < y(minvalue)
               
               minvalue = i;
           
           end
           
       end
       
   end
   
   % update current node, determined as the minimum distanced node
   % from previous current working node
   CurrentNode = minvalue;

   % increment counter
   nVisited = nVisited +1;

   t = t + 1;
end

return